package applogic;

import java.util.*;

enum MsgType{
	MEETING_NOTICE,
	ROOM_REQUEST,
	ROOM_ALLOCATION,
	LOGIN_REQUEST,
	LOGIN_RESULT,
	MEETING_STORAGE_REQUEST,
	APPOINTMENT_STORAGE_REQUEST,
	STORAGE_CONFIRMATION,
	RSVP,
	BROKEN,
	CALENDER_REQUEST,
	CALENDER_PAYLOAD,
	CALENDER_REQUEST_RESPONSE,
	DELETE_REQUEST,
	MEETING_UPDATE,
	CANCEL_CONFIRMATION,
	STORE_CONFIRMATION,
	DROPPED_NOTICE,
	DROPPED_CONFIRMATION,
	DECLINED_NOTICE,
	DROP_USERS
	};
public class Message {
	static HashMap<String,MsgType> msgtypes;
	public static void initialize()
	{
		msgtypes = new HashMap<String,MsgType>();
		msgtypes.put("meeting_notice",MsgType.MEETING_NOTICE );
		msgtypes.put("room_request",MsgType.ROOM_REQUEST );
		msgtypes.put("room_allocation",MsgType.ROOM_ALLOCATION );
		msgtypes.put("login_request",MsgType.LOGIN_REQUEST );
		msgtypes.put("login_result",MsgType.LOGIN_RESULT );
		msgtypes.put("meeting_storage_request",MsgType.MEETING_STORAGE_REQUEST );
		msgtypes.put("appointment_storage_request",MsgType.APPOINTMENT_STORAGE_REQUEST );
		msgtypes.put("storage_confirmation",MsgType.STORAGE_CONFIRMATION );
		msgtypes.put("rsvp",MsgType.RSVP);
		msgtypes.put("calender_request",MsgType.CALENDER_REQUEST);
		msgtypes.put("calender_payload",MsgType.CALENDER_PAYLOAD);
		msgtypes.put("calender_request_response", MsgType.CALENDER_REQUEST_RESPONSE);
		msgtypes.put("delete_request", MsgType.DELETE_REQUEST);
		msgtypes.put("meeting_update", MsgType.MEETING_UPDATE);
		msgtypes.put("cancel_confirmation",MsgType.CANCEL_CONFIRMATION);
		msgtypes.put("store_confirmation",MsgType.STORE_CONFIRMATION);
		msgtypes.put("dropped_notice",MsgType.DROPPED_NOTICE);
		msgtypes.put("dropped_confirmation",MsgType.DROPPED_CONFIRMATION);
		msgtypes.put("declined_notice",MsgType.DECLINED_NOTICE);
		msgtypes.put("drop_users",MsgType.DROP_USERS);
	}
	
	
	private String content;
	MsgType type;
	int IP;		
	
	public Message(){
		content = null;
		IP = 0;
		type = null;
	}
	
	public Message(String netMsg)
	{
		StringTokenizer st = new StringTokenizer(netMsg,",");
		String typeString = st.nextToken().toLowerCase();
		type = msgtypes.get(typeString);
		content = netMsg.substring(typeString.length());
		if(type==null)
		{
			System.err.println("Invalid message received:\n"+typeString+content);
			type = MsgType.BROKEN;
		}
	}
	
	public Message(MsgType type, String content, int IP)
	{
		this.type = type;
		this.content = content;
		this.IP = IP;
	}
	
	public MsgType getType()
	{
		return type;
	}
	
	public void setType(MsgType type)
	{
		this.type = type;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return content;
	}

	public void setTarget(int target) {
		this.IP = target;
	}

	public int getTarget() {
		return IP;
	}
	
	public String toString()
	{
		return type.toString().toLowerCase()+content;
	}
}